<?php
class Pupils_model extends CI_Model
{
	public function __construct()
	{
		$this->load->database();
	}	
	
	public function get_pupils($class = FALSE)
	{
		if ($class === FALSE)
		{
			$query = $this->db->get('pupils');
			return $query->result_array();
		}
		$query = $this->db->get_where('pupils', array('class' => $class));
		return $query->result_array();
	}	
	
	public function get_unregistered_pupils($class = FALSE,$date = FALSE)
	{
		$this->db->select('pupils.id,pupils.name AS name,pupils.surname AS surname, pupils.class AS pupilclass');
		$this->db->from('pupils');
		$where_not_exists = 'NOT EXISTS (SELECT 1 FROM entries WHERE `pupils`.`id` = `entries`.`pupil_id`';
		if ($date !== FALSE)
		{
			$date = date('Y-m-d');
			$where_not_exists .= ' AND `entries`.`date` = \'' . $date . '\'';
		}
		$where_not_exists .= ')';
		$this->db->where($where_not_exists, '', FALSE); 
		if ($class !== FALSE)
		{
			$this->db->where('pupils.class',$class);
		}
		$query = $this->db->get();
		return $query->result_array();		
	}	
	
	public function set_pupils()
	{
		$data = array(
			'name' => $this->input->post('name'),
			'surname' => $this->input->post('surname'),
			'class' => $this->input->post('class')
		);
		return $this->db->insert('pupils', $data);
	}

	public function add_pupil($pupil)
	{
		return $this->db->insert('pupils', $pupil);
	}

}